package tech.aistar.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.model.pojo.Result;

import javax.servlet.http.HttpServletRequest;

/**
 * 本类用来演示:spring - 注解
 * spring它是一个容器框架 , 将项目中的所有的javabean的生命周期全部交给Spring管理-默认是单例的
 *
 * 之前工厂模式 - 对象的创建和对象的使用分离
 *
 * @author: success
 * @date: 2021/4/28 9:27 上午
 */
@Controller
public class HelloResultController {

    @GetMapping("/hello")
    @ResponseBody
    public Result hello(){
        return new Result("300","helloworld体验success",null);
    }

    /**
     * http://localhost:8089/boot/hello2?username=tom&age=23
     * 接受数据
     * @return
     */
    @GetMapping("/hello2")
    @ResponseBody
    public Result hello2(String username,Integer age){
        System.out.println("username:"+username);
        System.out.println("age:"+age);
        return new Result("200","helloworld体验成功",null);
    }

//    @GetMapping("/user/index")
//    public String index(HttpServletRequest req){
//        req.setAttribute("username","admin");
//        return "/WEB-INF/user/index.jsp";
//    }

    @GetMapping("/user/index")
    public String index(Model model){
        model.addAttribute("username","tom");
        //return "/WEB-INF/user/index.jsp";
        return "user/index";
    }
}
